package org.jeecg.aspect;//package org.jeecg.aspect;
//
//import com.alibaba.fastjson.JSON;
//import com.alibaba.fastjson.JSONObject;
//import com.alibaba.fastjson.serializer.PropertyFilter;
//import lombok.extern.slf4j.Slf4j;
//import org.aspectj.lang.JoinPoint;
//import org.aspectj.lang.ProceedingJoinPoint;
//import org.aspectj.lang.annotation.Around;
//import org.aspectj.lang.annotation.Aspect;
//import org.aspectj.lang.annotation.Pointcut;
//import org.aspectj.lang.reflect.MethodSignature;
//import org.jeecg.common.api.dto.LogDTO;
//import org.jeecg.common.api.vo.Result;
//import org.jeecg.common.constant.CommonConstant;
//import org.jeecg.common.constant.enums.ModuleType;
//import org.jeecg.common.system.vo.LoginUser;
//import org.jeecg.common.util.*;
//
//import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
//import org.springframework.core.annotation.AnnotationUtils;
//import org.springframework.stereotype.Component;
//import org.springframework.validation.BindingResult;
//import org.springframework.web.bind.annotation.GetMapping;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.multipart.MultipartFile;
//
//import javax.servlet.ServletRequest;
//import javax.servlet.ServletResponse;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.lang.annotation.Annotation;
//import java.lang.reflect.Method;
//import java.util.Arrays;
//import java.util.Date;
//import java.util.Optional;
//
///**
// * @Description:
// * @Author: lizhaofei
// * @Date: 2023-06-15 15:16
// * @Version:
// */
//@Aspect
//@Component
//@Slf4j
//public class ThirdApiAspect {
//    @Pointcut("@within(org.springframework.cloud.openfeign.FeignClient)")
//    public void logPointCut() {
//
//    }
//
//    @Around("logPointCut()")
//    public Object around(ProceedingJoinPoint point) throws Throwable {
//        long beginTime = System.currentTimeMillis();
//        //执行方法
//        Object result = point.proceed();
//        //执行时长(毫秒)
//        long endTime = System.currentTimeMillis();
//        //保存日志
//        saveSysLog(point, beginTime,endTime, result);
//        return result;
//    }
//
//
//    private void saveSysLog(ProceedingJoinPoint joinPoint, long requestTime,long responseTime, Object result) {
//        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
//        Method method = signature.getMethod();
//        //请求的方法名
//        String className = joinPoint.getTarget().getClass().getName();
//        String methodName = signature.getName();
//        String targetUrl="";
//
//        Annotation[] annotations= method.getAnnotations();
//        if(annotations!=null){
////            System.out.println(JSON.toJSONString(annotations));
////            JSONObject jsonObject=JSON.parseObject(JSON.toJSONString(annotations[0]));
////            targetUrl=jsonObject.getString("value");
//            targetUrl=JSON.toJSONString(annotations);
//        }
//        //获取请求参数
//        String targetMethodParams=JacksonUtils.toJsonStringNonEmpty(joinPoint.getArgs());
//        BizLog bizLog=new BizLog();
//        bizLog.setBizName(methodName);
//        bizLog.setTargetUrl(targetUrl);
//        bizLog.setSourceIp("");
//        bizLog.setRequestBody(targetMethodParams);
//        bizLog.setResponseBody(JacksonUtils.toJsonStringNonEmpty(result));
//        bizLog.setLogTime(DateUtils.formatDate(new Date(),"yyyy-mm-dd MM:HH:SS"));
//        bizLog.setResponseTime(responseTime);
//        bizLog.setRequestTime(requestTime);
//        bizLog.setUseTime(responseTime-requestTime);
//        BizLogUtil.writeLog(bizLog);
//    }
//
//
//    /**
//     * @Description: 获取请求参数
//     * @author: scott
//     * @date: 2020/4/16 0:10
//     * @param request:  request
//     * @param joinPoint:  joinPoint
//     * @Return: java.lang.String
//     */
//    private String getReqestParams(HttpServletRequest request, JoinPoint joinPoint) {
//        String httpMethod = request.getMethod();
//        String params = "";
//        if ("POST".equals(httpMethod) || "PUT".equals(httpMethod) || "PATCH".equals(httpMethod)) {
//            Object[] paramsArray = joinPoint.getArgs();
//            // java.lang.IllegalStateException: It is illegal to call this method if the current request is not in asynchronous mode (i.e. isAsyncStarted() returns false)
//            //  https://my.oschina.net/mengzhang6/blog/2395893
//            Object[] arguments  = new Object[paramsArray.length];
//            for (int i = 0; i < paramsArray.length; i++) {
//                if (paramsArray[i] instanceof BindingResult || paramsArray[i] instanceof ServletRequest || paramsArray[i] instanceof ServletResponse || paramsArray[i] instanceof MultipartFile) {
//                    //ServletRequest不能序列化，从入参里排除，否则报异常：java.lang.IllegalStateException: It is illegal to call this method if the current request is not in asynchronous mode (i.e. isAsyncStarted() returns false)
//                    //ServletResponse不能序列化 从入参里排除，否则报异常：java.lang.IllegalStateException: getOutputStream() has already been called for this response
//                    continue;
//                }
//                arguments[i] = paramsArray[i];
//            }
//            //update-begin-author:taoyan date:20200724 for:日志数据太长的直接过滤掉
//            PropertyFilter profilter = new PropertyFilter() {
//                @Override
//                public boolean apply(Object o, String name, Object value) {
//                    if(value!=null && value.toString().length()>500){
//                        return false;
//                    }
//                    return true;
//                }
//            };
//            params = JSONObject.toJSONString(arguments, profilter);
//            //update-end-author:taoyan date:20200724 for:日志数据太长的直接过滤掉
//        } else {
//            MethodSignature signature = (MethodSignature) joinPoint.getSignature();
//            Method method = signature.getMethod();
//            // 请求的方法参数值
//            Object[] args = joinPoint.getArgs();
//            // 请求的方法参数名称
//            LocalVariableTableParameterNameDiscoverer u = new LocalVariableTableParameterNameDiscoverer();
//            String[] paramNames = u.getParameterNames(method);
//            if (args != null && paramNames != null) {
//                for (int i = 0; i < args.length; i++) {
//                    params += "  " + paramNames[i] + ": " + args[i];
//                }
//            }
//        }
//        return params;
//    }
//
//
//}
